package cn.ycc1;

import java.util.LinkedList;

/**
 * https://leetcode.cn/problems/intersection-of-two-linked-lists/description/
 *
 * @author ycc
 * @date 2024/3/23
 *
 * Definition for singly-linked list.
 * public class ListNode {
 *     int val;
 *     ListNode next;
 *     ListNode(int x) {
 *         val = x;
 *         next = null;
 *     }
 * }
 * [4,1,8,4,5]
 * [5,6,1,8,4,5]
 */
public class _160_相交链表 {
    public static void main(String[] args) {

    }

    public static ListNode getIntersectionNode(ListNode headA, ListNode headB) {
        if(headA == null || headB == null) {
            return null;
        }
        ListNode curA = headA;
        ListNode curB = headB;
        while (curA != curB) {
            curA = (curA == null) ? headB : curA.next;
            curB = (curB == null) ? headA : curB.next;
        }
        return curA;
    }
}


